package com.fanrenqiji.modules.sys.entity;

import com.fanrenqiji.common.Collections;
import com.fanrenqiji.common.persistence.DataEntity;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.google.common.collect.Lists;
import org.hibernate.annotations.*;
import org.hibernate.validator.constraints.Email;
import org.hibernate.validator.constraints.Length;

import javax.persistence.*;
import javax.persistence.Entity;
import javax.persistence.OrderBy;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import java.util.Date;
import java.util.List;

/**
 * @author lijian E-mail:ljz0721cx@163.com
 * @version 创建时间：2015年1月15日 上午11:20:00
 * @description 基本的管理用户信息表 引入了hibernate的@DynamicInsert @DynamicUpdate
 *              该只是试用在业务中需要修改
 */
@Entity(name = "User")
@Table(name = "sys_user")
@Cacheable(true)
@DynamicInsert
@DynamicUpdate
public class User extends DataEntity {

	private static final long serialVersionUID = 1L;
	
	private static final String ACTIVE = "ACTIVE";
	
	private static final String INACTIVE = "INACTIVE";

	private Long id;
	/**
	 * 登录名，必须唯一(可能作为登录)
	 */
	private String loginName;
	/**
	 * 邮箱，必须唯一(可能作为登录)
	 */
	private String email;
	/**
	 * 手机 ,必须唯一(可能作为登录)
	 */
	private String mobile;
	/**
	 * 密码(使用独立密码，将salt和登录通道的耦合解除)
	 */
	private String password;
	/**
	 * 员工编号
	 */
	private String no;
	/**
	 * 姓名
	 */
	private String name;
	/**
	 * 所属通路(和所属部门是上下关系)
	 */
	private Office company;
	/**
	 * 所属部门
	 */
	private Office office;
	/**
	 * 电话
	 */
	private String phone;
	/**
	 * 用户类型
	 */
	private String userType;
	/**
	 * 最后登录的IP地址
	 */
	private String loginIp;
	/**
	 * 最后登录的日期
	 */
	private Date loginDate;
	
	/**
	 * 用户状态 ：ACTIVE :激活  INACTIVE 禁用
	 */
	 private String userState;
	 
	 
	 
	 
    @Column(name = "user_state")
	public String getUserState() {
		return userState;
	}

	public void setUserState(String userState) {
		this.userState = userState;
	}

	private List<Role> roleList = Lists.newArrayList(); // 拥有角色列表

	public User() {
		super();
		//初始化时候默认用户为激活状态
		this.userState = ACTIVE;
	}

	public User(Long id) {
		this();
		this.id = id;
	}

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	@Length(min = 5, max = 20,message="登录名长度必须介于 5 到 20 之间")
	@Column(name = "login_name", unique = true,updatable=false,nullable=false)
	@Pattern(regexp = "^\\w{4,20}$", message = "用户名必须以字母开头，可由字母数字下划线组成")
	public String getLoginName() {
		return loginName;
	}

	public void setLoginName(String loginName) {
		this.loginName = loginName;
	}

	@Length(min = 1, max = 100,message="邮箱长度必须介于 1 和 100 之间")
	@Email(message="邮箱格式不正确")
	@Column(name = "email", unique = true,nullable=true)
	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	//@Length(min = 11, max = 13)
	@Column(name = "mobile", length = 20, unique = true,nullable=true)
	public String getMobile() {
		return mobile;
	}

	public void setMobile(String mobile) {
		this.mobile = mobile;
	}

	@Length(min = 8, max = 100)
	@Column(name = "password")
	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	@Length(min = 5, max = 50,message="工号长度必须介于 5 和 50 之间")
	@Column(name = "no", length = 50)
	public String getNo() {
		return no;
	}

	public void setNo(String no) {
		this.no = no;
	}

	@Length(min = 2, max = 50)
	@Column(name = "name")
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Column(name = "phone",nullable=true)
	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	@Length(min = 1, max = 13)
	@Column(name = "user_type", length = 20)
	public String getUserType() {
		return userType;
	}

	public void setUserType(String userType) {
		this.userType = userType;
	}

	@Length(min = 1, max = 50)
	@Column(name = "login_ip", length = 20)
	public String getLoginIp() {
		return loginIp;
	}

	public void setLoginIp(String loginIp) {
		this.loginIp = loginIp;
	}

	@Column(name = "login_date")
	public Date getLoginDate() {
		return loginDate;
	}

	public void setLoginDate(Date loginDate) {
		this.loginDate = loginDate;
	}

	@ManyToOne
	@JoinColumn(name = "company_id")
	@NotFound(action = NotFoundAction.IGNORE)
	@JsonIgnore
	@NotNull(message = "归属公司不能为空")
	public Office getCompany() {
		return company;
	}

	public void setCompany(Office company) {
		this.company = company;
	}

	@ManyToOne
	@JoinColumn(name = "office_id")
	@NotFound(action = NotFoundAction.IGNORE)
	@JsonIgnore
	@NotNull(message = "归属部门不能为空")
	public Office getOffice() {
		return office;
	}

	public void setOffice(Office office) {
		this.office = office;
	}

	@ManyToMany(fetch = FetchType.EAGER)
	@JoinTable(name = "sys_user_role", joinColumns = { @JoinColumn(name = "user_id") }, inverseJoinColumns = { @JoinColumn(name = "role_id") })
	@Where(clause = "del_flag='" + DEL_FLAG_NORMAL + "'")
	@OrderBy("id")
	@Fetch(FetchMode.SUBSELECT)
	@NotFound(action = NotFoundAction.IGNORE)
	@JsonIgnore
	public List<Role> getRoleList() {
		return roleList;
	}

	public void setRoleList(List<Role> roleList) {
		this.roleList = roleList;
	}

	//判断是否是超管
	@Transient
	public boolean isAdmin() {
		return isAdmin(this.id);
	}

	@Transient
	public static boolean isAdmin(Long id) {
		return id != null && id.equals(1L);
	}
	
	
	/**
	 * 用户拥有的角色名称字符串, 多个角色名称用','分隔.
	 */
	@Transient
	public String getRoleNames() {
		return Collections.extractToString(roleList, "name", ", ");
	}
	/**
	 * 获得当前用户角色的Id的list
	 * @return
	 */
	@Transient
	@JsonIgnore
	public List<Long> getRoleIdList() {
		List<Long> roleIdList = Lists.newArrayList();
		for (Role role : roleList) {
			roleIdList.add(role.getId());
		}
		return roleIdList;
	}

	public void setRoleIdList(List<Long> roleIdList) {
		roleList = Lists.newArrayList();
		for (Long roleId : roleIdList) {
			Role role = new Role();
			role.setId(roleId);
			roleList.add(role);
		}
	}
}
